﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SistemaComprasVentaYICHAN
{
    public partial class GenerarCotizacionCompraDLG : Form
    {
        List<LineaCotizacionCompra> lineas = new List<LineaCotizacionCompra>();
        private double total;
        GestorCotizacionCompra gesCot = GestorCotizacionCompra.obtenerInstancia;

        public GenerarCotizacionCompraDLG()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {
            CatalogoProductosDLG catProd = new CatalogoProductosDLG();
            catProd.ShowDialog();

            //dataGridView1.Rows.Clear();

            for (int i = 0; i < catProd.lista.Count; i++)
            {
                string[] fila = { catProd.lista[i].CodigoProveedor1.ToString(), catProd.lista[i].Nombre1, catProd.lista[i].PrecioBaseCompra1.ToString() };
                dataGridView1.Rows.Add(fila);
            }

        }

        private void label3_Click_1(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            Int32 RowCount = dataGridView1.Rows.Count;
            LineaCotizacionCompra linea;

            int codCot = Convert.ToInt32(textBox1.Text);
            string estado = comboBox1.Text;
            DateTime fecha = dateTimePicker1.Value;
            double tot = total;

            for (int i = 0; i < RowCount; i++)
            {
                linea = obtenerLinea(i);
                lineas.Add(linea);
            }

            gesCot.agregarCotizacion(codCot, fecha, tot, estado, lineas);

            MessageBox.Show("Cotizacion Agregada con exito");

        }

        private void button6_Click(object sender, EventArgs e)
        {
            Close();
        }

        private LineaCotizacionCompra obtenerLinea(int i)
        {
            LineaCotizacionCompra line;

            DataGridViewRow row = dataGridView1.Rows[i];

            int numLinea = row.Index;
            string producto = Convert.ToString(row.Cells["nombreProducto"].Value);
            int cant = Convert.ToInt32(row.Cells["cant"].Value);
            double pu = Convert.ToDouble(row.Cells["PrecioUnitario"].Value);
            double subtotal = Convert.ToDouble(row.Cells["subtotal"].Value);

            line = new LineaCotizacionCompra(numLinea, producto, cant, pu, subtotal);
            return line;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            total = 0;

            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                DataGridViewRow row = dataGridView1.Rows[i];
                total += Convert.ToDouble(row.Cells["subtotal"].Value);
            }
            textBox2.Text = total.ToString();
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];

            int cant = Convert.ToInt32(row.Cells["cant"].Value);
            double precio = Convert.ToDouble(row.Cells["PrecioUnitario"].Value);

            row.Cells["subtotal"].Value = cant * precio;
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {

        }

    }
}
